 //
//  WBHomeViewController.swift
//  微博
//
//  Created by 胡宁 on 2016/12/10.
//  Copyright © 2016年 胡宁. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {
    
    lazy var statusList = [String]()
    
    /// 加载数据
    override func loadData() {
        
        print("开始加载数据。。。")
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            
            for i in 0..<15 {
                if self.isPullup {
                    //将数据追加到底部
                    self.statusList.append("上拉 \(i)")
                }else {
                    //数据插入到数组顶部
                    self.statusList.insert(i.description, at: 0)
                }
                
            }
            
            //结束刷新控件
            self.refreshCtrol?.endRefreshing()
            //恢复下拉刷新标记
            self.isPullup = false
            //刷新表格
            self.tableView?.reloadData()
        }
        
        
    }
    
    @objc func showFriend() {
        print(#function)
        let vc = WBDemoViewController()
        
        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

 
// 表格数据源方法
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
    }
    
}
 
// 设置界面
extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        //设置导航
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriend))
        
        //注册原型 cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
}
